# -*- encoding: utf-8 -*-
#import odoo.addons.decimal_precision as dp
from odoo import models, fields, api, _
from odoo.addons import base_cw
from odoo.exceptions import UserError
from odoo.tools.float_utils import float_round, float_compare


class account_balance_sheet(models.Model):
    _name = 'account.balance.sheet'
    _description = u'资产负债表'

    date = fields.Date(u'日期')
    sequence = fields.Integer(u'项次')
    asset_sequence = fields.Char(u'行次')
    asset_name = fields.Char(u'资产')
    asset_begin = fields.Float(u'年初余数', digits='Amount')
    asset_balance = fields.Float(u'期末余数', digits='Amount')
    liability_sequence = fields.Char(u'行次')
    liability_name = fields.Char(u'负债及权益类')
    liability_begin = fields.Float(u'年初余数', digits='Amount')
    liability_balance = fields.Float(u'期末余数', digits='Amount')
    master_id = fields.Many2one('account.report.format.designer', u'主档',ondelete="cascade")

    def init(self):
        self._cr.execute("""
                    CREATE OR REPLACE FUNCTION account_balance_sheet_write(
                            to_date timestamp,
                            master_id_t integer,
                            uid_t integer)
                      RETURNS void AS
                    $BODY$
                    /*资产负债表
                    --DROP FUNCTION account_balance_sheet_write(timestamp,integer,integer)
                    -- select account_balance_sheet_write('2016-09-22',1,1)
                    -- select *  from account_balance_sheet
                    -- delete from account_balance_sheet

                    */
                    declare
                           assets_name character varying;
                           assets_sequence character varying;
                           assets_begin numeric;
                           assets_balance numeric;
                           liability_name character varying;
                           liability_sequence character varying;
                           liability_begin numeric;
                           liability_balance numeric;
                           max_seq integer:=0;
                           seq integer:=0;
                           sequence_t integer;
                    begin
                        create temp table designer_line_t(
                                sequence integer,
                                name character varying,
                                print_sequence character varying,
                                e1 numeric,
                                a1 numeric
                        );

                        insert into designer_line_t(sequence,name,print_sequence,e1,a1)
                        select a.sequence,a.name,a.print_sequence,a.e1,a.a1
                          from  account_report_format_designer_line a left join account_report_format_designer b on a.master_id=b.id
                         where b.id=master_id_t;

                        select max(sequence)/2 into  seq  from  account_report_format_designer_line a left join account_report_format_designer b on a.master_id=b.id
                        where b.id=master_id_t;

                        sequence_t:=1;
                            max_seq:=seq;
                        --raise notice 'max_seq:%,seq:%',max_seq/2,seq;
                        delete from account_balance_sheet where master_id=master_id_t;
                        while seq<>0
                        loop
                            select  name,print_sequence,e1,a1 into assets_name,assets_sequence,assets_begin,assets_balance
                              from designer_line_t
                             where sequence=sequence_t;

                             select  name,print_sequence,e1,a1 into liability_name,liability_sequence,liability_begin,liability_balance
                              from designer_line_t
                             where sequence=max_seq+sequence_t;

                            insert into account_balance_sheet(date,sequence,master_id,
                                     asset_name,asset_sequence,asset_begin,asset_balance,
                                     liability_name,liability_sequence,liability_begin,liability_balance,
                                     create_uid,create_date)
                            values(to_date,sequence_t,master_id_t,
                                   assets_name,assets_sequence,assets_begin,assets_balance,
                                   liability_name,liability_sequence,liability_begin,liability_balance,
                                   uid_t,(now() at time zone 'UTC'));
                            sequence_t:=sequence_t+1;
                            seq:=seq-1;
                            --raise notice 'seq:%,sequence_t:%',seq,sequence_t;
                        end loop;

                        drop table designer_line_t;
                    end;
                    $BODY$
                    LANGUAGE plpgsql VOLATILE
                    COST 100;

                   """)
